package com.gigmissions.client.callbacks;

import com.gigmissions.client.config.GigMissionsConstants;
import com.gigmissions.client.config.GigMissionsMessages;
import com.gigmissions.client.dto.LoginInfo;
import com.gigmissions.client.ui.HomePanel;
import com.gigmissions.client.ui.LoginSignupPanel;
import com.gigmissions.client.ui.ProfilePanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class LoginSignupCallbackHandler implements AsyncCallback<LoginInfo>{

	
	private GigMissionsMessages messages;
	private GigMissionsConstants constants;
	private ProfilePanel profilePanel;
	private LoginSignupPanel loginSignupPanel;
	private HomePanel homePanel;

	
	@Inject
	public LoginSignupCallbackHandler(HomePanel homePanel,
									ProfilePanel profilePanel,
									GigMissionsMessages messages,
									GigMissionsConstants constants, 
									LoginSignupPanel loginSignupPanel){
		this.messages = messages;
		this.homePanel = homePanel;
		this.constants = constants;
		this.loginSignupPanel = loginSignupPanel;
		this.profilePanel = profilePanel;
	}
	
	
	
	@Override
	public void onFailure(Throwable caught) {
		GWT.log("Error signing up!", caught);
		Window.alert(messages.loginSignupError(constants.errorCodeLogin()));
		
	}

	@Override
	public void onSuccess(LoginInfo result) {
		//@TODO: fix this
		if(result.isLoggedIn()){
			//present
			loginSignupPanel.showLoggedIn(result.getLogoutUrl(), result.getNickname());
			//homePanel.addAddGigAnchor();
			profilePanel.setVisible(true);
		}else{
			loginSignupPanel.showLoggedOut(result.getLoginUrl());
			profilePanel.setVisible(false);
			
		}
	}



	
	
}